/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.md.album.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.living.filedownloader.DownLoadManger;
import com.living.filedownloader.ui.TasksDownloadingActivity;
import com.living.player.db.RecentlyManger;
import com.living.widget.BottomMenuView;
import com.md.album.R;
import com.md.album.db.SelfAlbumManager;
import com.md.album.event.SelfRefreshEvent;
import com.md.basedpc.routerservice.NotifyService;
import com.md.livingwidget.CoreBaseActivity;
import com.md.livingwidget.toast.PHToast;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import static com.md.basedpc.constant.BaseConstant.SELF_CREATE_ALBUM;

/**
 * 已下载 批量的操作
 * 操作
 */
public abstract class AbstractSelectAudioActivity extends CoreBaseActivity {
    public static final String SELF_ALBUM_ID="SELF_ALBUM_ID";
    public static final String SELF_ALBUM = "SELF_ALBUM";
    protected BottomMenuView bottom_menu;
    protected AbstractSelectAudioFragment audioBatchFragment;
    protected AbstractSelectAudioActivity currentActivity;
    protected String albumId ;
    protected Album album;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        albumId = getIntent().getStringExtra(SelfAudioBatchActivity.SELF_ALBUM_ID);
        initFragment();
        bottom_menu = findViewById(R.id.bottom_menu);
        showMenu();
        bottom_menu.setMenuAddEnable(false);
        bottom_menu.setMenuDownloadEnable(false);
        bottom_menu.setMenuShareEnable(false);
        bottom_menu.setMenuDeleteEnable(false);

        audioBatchFragment.setOnBatchSelectLisenter(new SelfAudioBatchFragment.OnBatchSelectLisenter() {
            @Override
            public void onSelect(int count, int total) {
                if (count > 0) {
                    bottom_menu.setMenuAddEnable(true);
                    bottom_menu.setMenuDownloadEnable(true);
                    bottom_menu.setMenuShareEnable(true);
                    bottom_menu.setMenuDeleteEnable(true);
                } else {
                    bottom_menu.setMenuAddEnable(false);
                    bottom_menu.setMenuDownloadEnable(false);
                    bottom_menu.setMenuShareEnable(false);
                    bottom_menu.setMenuDeleteEnable(false);
                }
            }
        });
        bottom_menu.setOnBottomMenuViewClick(new BottomMenuView.OnBottomMenuViewClick() {
            @Override
            public void onClickAdd(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                ArrayList<Audio> selectedAudios = new ArrayList<>();
                for (Audio audio : mapSel.values()) {
                    selectedAudios.add(audio);
                }
                Intent intent = new Intent(currentActivity,AlbumListActivity.class);
                intent.putParcelableArrayListExtra(AlbumListActivity.SELECTED_AUDIOS,selectedAudios);
                startActivity(intent);
            }

            @Override
            public void onClickDownload(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                if (mapSel != null && mapSel.size()>0){
                    ArrayList<Audio> audios = new ArrayList<>();
                    for (Audio audio : mapSel.values()){
                        audios.add(audio);
                    }
                    if (album != null){
                        DownLoadManger.getInstance().downloadAudioList(album,audios);
                    }else {
                        DownLoadManger.getInstance().downloadAudioList(audios);
                    }
                    PHToast.showSingleToast(getmActivity(), getString(R.string.album_in_download_list));
                }
            }

            @Override
            public void onClickShare(View view) {
                PHToast.showSingleToast(getmActivity(), "分享组件");
            }

            @Override
            public void onClickDelete(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                if (mapSel.size() > 0) {
                    setResult(Activity.RESULT_OK);
                    //遍历map中的值
                    for (Audio value : mapSel.values()) {
                        RecentlyManger.getInstance().deleteAudio(value.getId());
                        mapSel.remove(value);
                        audioBatchFragment.remove(value);
                        if (!TextUtils.isEmpty(albumId)){
                            SelfAlbumManager.getIns().deleteAudioFromSelfAlbum(albumId,value.getId());
                        }else {
                            SelfAlbumManager.getIns().deleteAudio(value.getId());
                        }
                    }
//                    EventBus.getDefault().post(new SelfRefreshEvent());
                    ARouter.getInstance().navigation(NotifyService.class).selfAlbumUpdateSuc(SELF_CREATE_ALBUM);
                } else {
                    PHToast.showSingleToast(getmActivity(), "请选择要删除的");
                }
            }
        });
    }

    protected abstract int getLayoutRes();
    protected abstract void initFragment();
    protected abstract void showMenu();

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.layout_downloading) {
            startActivity(new Intent(this, TasksDownloadingActivity.class));
        } else if (v.getId() == R.id.iv_downloaded_edite) {

        }
    }

}
